/**
 * <p>Title: MaterialAnalysisPropertyDaoImpl</p>
 * <p>Description: MaterialAnalysisPropertyDao 实现类</p>
 * <p>Copyright: Shanghai Batchsight Pharmaceutical Technologies, Inc. Copyright(c) 2016</p>
 * @author Wan shuaishuai
 * @version 1.0
 * <pre>Histroy:
 *       2017-02-19  Wan shuaishuai
 *</pre>
 */
package cn.jhg.account.persist;

import javax.annotation.Resource;

import org.bson.Document;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import cn.jhg.account.entity.MaterialAnalysis;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

@Component
public class MaterialAnalysisPropertyDaoImpl implements MaterialAnalysisPropertyDao {
	@Resource
	private MongoTemplate mongoTemplate;
	Logger logger=LoggerFactory.getLogger(MaterialAnalysisPropertyDaoImpl.class);
	/**
	 * 添加分析成分
	 * @author Wan shuaishuai
	 * @param materialAnalysis 原料分析实体
	 * @return
	 */
	@Override
	public int addAnalysisProperty(MaterialAnalysis materialAnalysis) {
		try{
			DBObject object = new BasicDBObject();//设置需要插入到数据库的文档对象
			ObjectId propId = new ObjectId(materialAnalysis.getCompId());
			object.put("_id", propId);
			object.put("materialAnalysis", materialAnalysis);
			mongoTemplate.insert(object,"material_analysis_property");
			logger.info("[INF230701916] 添加物料分析成分成功");
			return 0;//物料分析成分添加成功
		}catch(Exception e){
			logger.error("[ERR230707750] 异常:添加物料分析成分失败",e.getMessage());
			return 230707750;//异常错误代码
		}
	}

	/**
	 * 更新分析成分
	 * @author Wan shuaishuai
	 * @param materialAnalysis  原料分析实体
	 * @return 更新条数
	 */
	@Override
	public int updateAnalysisProperty(MaterialAnalysis materialAnalysis) {
		try {
			if(null==materialAnalysis.getCompId()){
				logger.error("[ERR230708329] 物料分析成分id为空,更新失败");
				return 230708329; //物料分析成id为空,更新失败
			}
			Query query = new Query();
			query.addCriteria(Criteria.where("_id").is(materialAnalysis.getCompId()));
			Update update = new Update();
			update.set("materialAnalysis",materialAnalysis);
			mongoTemplate.updateMulti(query, update, Document.class, "material_analysis_property");
			logger.info("[INF230710771] 更新物料分析成分成功");
			return 0;
		} catch (Exception e) {
			logger.error("[ERR230710808] 异常:更新物料分析成分失败",e.getMessage());
			return 230710808;//更新失败
		}
	}

	/**
	 * 查询MaterialAnalysis
	 * @author Wan shuaishuai
	 * @param id 分析成分id
	 * @return Document 分析成分数据
	 */
	@Override
	public MaterialAnalysis queryAnalysisPropertyById(ObjectId id) {
		try {
			Document doc = mongoTemplate.findById(id, Document.class, "material_analysis_property");
			MaterialAnalysis materialAnalysis  = (MaterialAnalysis)doc.get("materialAnalysis");
			logger.info("[INF230710863] 通过id查询原料分析成分成功");
			return materialAnalysis;
		} catch (Exception e) {
			logger.error("[ERR230710897] 异常:通过id查询原料分析成分失败",e.getMessage());
			return null; //通过id查询原料分析成分失败
		}
	}
	
	
}
